/*
 * Copyright (C) 2008 Universidade Federal de Campina Grande
 *  
 * This file is part of OurGrid. 
 *
 * OurGrid is free software: you can redistribute it and/or modify it under the
 * terms of the GNU Lesser General Public License as published by the Free 
 * Software Foundation, either version 3 of the License, or (at your option) 
 * any later version. 
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
 * for more details. 
 * 
 * You should have received a copy of the GNU Lesser General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package org.ourgrid.peer.ui.async.gui;

import java.awt.Frame;

import javax.swing.ActionMap;
import javax.swing.JFrame;

import org.ourgrid.peer.ui.async.gui.actions.AddPeerUserAction;
import org.ourgrid.peer.ui.async.gui.actions.AddWorkerAction;
import org.ourgrid.peer.ui.async.gui.actions.InstallPeerAsServiceAction;
import org.ourgrid.peer.ui.async.gui.actions.SetWorkersAction;
import org.ourgrid.peer.ui.async.gui.actions.StartPeerAction;
import org.ourgrid.peer.ui.async.gui.actions.StopPeerAction;
import org.ourgrid.peer.ui.async.gui.actions.UninstallPeerServiceAction;

/**
 * Panel that provide the actions to the peer.
 */
public class ActionsPanel extends javax.swing.JPanel {
    
	private static final long serialVersionUID = 1L;
	
	private static final String ADD_PEER_USER_ACTION_NAME = "peer.addPeerUser.action";
	private static final String ADD_WORKER_ACTION_NAME = "peer.addWorker.action";
	//private static final String ADD_XMPP_USER_ACTION_NAME = "peer.addXMPPUser.action";
	private static final String INSTALL_SERVICE_ACTION = "peer.install.action";
	private static final String UNINSTALL_SERVICE_ACTION = "peer.uninstall.action";
	
    private StartPeerAction startPeerAction;
    private StopPeerAction stopPeerAction;
    private AddWorkerAction addWorkerAction;
    private SetWorkersAction setWorkersAction;
    private AddPeerUserAction addPeerUserAction;
    //private AddXMPPUserAction addXMPPUserAction;
	private InstallPeerAsServiceAction installAction;
	private UninstallPeerServiceAction uninstallAction;

	/** Creates new form ActionsPanel */
    public ActionsPanel(Frame frame) {
    	initComponents();
        initActions(frame);
    }

    /**
     * Initializes the Actions that will appear at this panel.
     * @param frame The frame where this panel is, it is necessary to open
     * other panels on it.
     * @param componentClient The client of the peer.
     */
    private void initActions(Frame frame) {
    	
    	JFrame jFrame = (JFrame) frame;
        startPeerAction = new StartPeerAction(jFrame.getContentPane());
        controlTaskPane.add(startPeerAction);
        stopPeerAction = new StopPeerAction(jFrame.getContentPane());
        controlTaskPane.add(stopPeerAction);
        addWorkerAction = new AddWorkerAction(frame);
        workersTaskPane.add(addWorkerAction);
        setWorkersAction = new SetWorkersAction(frame);
        workersTaskPane.add(setWorkersAction);
        addPeerUserAction = new AddPeerUserAction(frame);
        peerUsersTaskPane.add(addPeerUserAction);
        //addXMPPUserAction = new AddXMPPUserAction(frame);
        //xmppTaskPane.add(addXMPPUserAction);
        
    	installAction = new InstallPeerAsServiceAction();
    	uninstallAction = new UninstallPeerServiceAction();
    	otherOperationsPane.add(installAction);
    	otherOperationsPane.add(uninstallAction);
	}
    
    /**
     * Enable the actions that are permited when the
     * peer has been started.
     */
    public void peerStarted() {
        startPeerAction.setEnabled(false);
        stopPeerAction.setEnabled(true);
        addPeerUserAction.setEnabled(true);
        setWorkersAction.setEnabled(true);
        addWorkerAction.setEnabled(true);
    }
    
    /**
     * Enable the actions that are permited when the
     * peer has been stopped.
     */
    public void peerStopped() {
        startPeerAction.setEnabled(true);
        stopPeerAction.setEnabled(false);
        addPeerUserAction.setEnabled(false);
        setWorkersAction.setEnabled(false);
        addWorkerAction.setEnabled(false);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jXTaskPaneContainer1 = new org.jdesktop.swingx.JXTaskPaneContainer();
        controlTaskPane = new org.jdesktop.swingx.JXTaskPane();
        workersTaskPane = new org.jdesktop.swingx.JXTaskPane();
        peerUsersTaskPane = new org.jdesktop.swingx.JXTaskPane();
        //xmppTaskPane = new org.jdesktop.swingx.JXTaskPane();
        otherOperationsPane = new org.jdesktop.swingx.JXTaskPane();

        controlTaskPane.setTitle("Control"); // NOI18N
        jXTaskPaneContainer1.add(controlTaskPane);

        workersTaskPane.setTitle("Workers"); // NOI18N
        jXTaskPaneContainer1.add(workersTaskPane);
        
        peerUsersTaskPane.setTitle("Peer users");
        jXTaskPaneContainer1.add(peerUsersTaskPane);
        
        //xmppTaskPane.setTitle("XMPP");
        //jXTaskPaneContainer1.add(xmppTaskPane);

        otherOperationsPane.setTitle("Other");
        jXTaskPaneContainer1.add(otherOperationsPane);
        
        ActionMap actionMap = new ActionMap();
        actionMap.put(ADD_PEER_USER_ACTION_NAME, addPeerUserAction);
        actionMap.put(ADD_WORKER_ACTION_NAME, addWorkerAction);
        //actionMap.put(ADD_XMPP_USER_ACTION_NAME, addXMPPUserAction);
        actionMap.put(INSTALL_SERVICE_ACTION, installAction);
        actionMap.put(UNINSTALL_SERVICE_ACTION, uninstallAction);
        
        jXTaskPaneContainer1.setActionMap(actionMap);
        
        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jXTaskPaneContainer1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jXTaskPaneContainer1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    
	public void setStartEnabled(boolean enabled) {
		startPeerAction.setEnabled(enabled);
	}
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.jdesktop.swingx.JXTaskPane controlTaskPane;
    private org.jdesktop.swingx.JXTaskPaneContainer jXTaskPaneContainer1;
    private org.jdesktop.swingx.JXTaskPane peerUsersTaskPane;
    private org.jdesktop.swingx.JXTaskPane workersTaskPane;
    //private org.jdesktop.swingx.JXTaskPane xmppTaskPane;
    private org.jdesktop.swingx.JXTaskPane otherOperationsPane;
    // End of variables declaration//GEN-END:variables
    
}
